﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Modules.factory.ui {
    import flash.events.*;
    import Engine.Events.*;
    import flash.display.*;
    import Events.*;
    import flash.utils.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Classes.util.*;
    import org.aswing.geom.*;
    import Classes.inventory.*;
    import Modules.stats.experiments.*;
    import Display.GateUI.*;

    public class FactoryGrowStateDialog extends GenericDialog {

        protected static const GRIDCELL_FACTORY:Class = KeyCellCrewFactory;

        protected var numAssets:int = 1;
        protected var m_factory:Factory;
        protected var m_finishCheckCallback:Function;
        protected var m_finishCallback:Function;
        protected var m_buyAllCallback:Function;
        protected var m_contractPic:ItemImageInstance;

        public function FactoryGrowStateDialog(_arg1:Factory, _arg2:String, _arg3:String, _arg4:Function, _arg5:Function, _arg6:Function){
            this.m_factory = _arg1;
            this.m_finishCheckCallback = _arg4;
            this.m_finishCallback = _arg5;
            this.m_buyAllCallback = _arg6;
            super(_arg3, "", 0, null, _arg2);
        }
        public function refresh():void{
            var _local1:FactoryGrowStateDialogView = (m_jpanel as FactoryGrowStateDialogView);
            if (_local1){
                _local1.update(this.m_factory);
            };
        }
        override protected function loadAssets():void{
            this.m_contractPic = this.m_factory.harvestingDefinition.getCachedImage("growStateDialog");
            if (!this.m_contractPic){
                this.numAssets++;
                this.m_factory.harvestingDefinition.addEventListener(LoaderEvent.LOADED, this.onContractPicLoaded, false, 0, true);
            };
            Global.delayedAssets.get(DelayedAssetLoader.BUILDABLE_ASSETS, this.onLoadBuildableAssets);
        }
        override protected function createAssetDict():Dictionary{
            var _local1:Dictionary = new Dictionary();
            _local1["dialog_bg"] = (new m_comObject.factory_hireWorkers_bg() as DisplayObject);
            _local1["cell_bg"] = m_comObject.crew_position_bg;
            _local1["cell_bg_alt"] = m_comObject.crew_position_bg_alternate;
            _local1["cell_goods"] = m_comObject.factory_hireWorkers_goods_icon_small;
            _local1["pic_timer"] = (new m_comObject.factory_hireWorkers_timer_icon() as DisplayObject);
            _local1["pic_goods"] = (new m_comObject.factory_hireWorkers_goods_icon() as DisplayObject);
            _local1["pic_contract"] = (this.m_contractPic.image as DisplayObject);
            _local1["pic_cashBtn"] = (new EmbeddedArt.icon_cash_big() as DisplayObject);
            _local1["stringAssets"] = {
                dialogTitle:ZLoc.t("Dialogs", (m_title + "_title")),
                jobTitle:this.m_factory.harvestingDefinition.localizedName,
                jobDescription:this.getJobDescription(),
                workerBonusText:("+" + Global.player.GetDooberMinimums(this.m_factory.harvestingDefinition, Commodities.PREMIUM_GOODS_COMMODITY)),
                hireFriendsButton:ZLoc.t("Dialogs", "HireFriendsButtonLabel"),
                buyWorkerButton:ZLoc.t("Dialogs", "BuyCrewButtonLabel", {amount:this.m_factory.harvestingDefinition.workers.cashCost.toString()}),
                completeButton:ZLoc.t("Dialogs", "OkButton"),
                unlockWorkerQuestion:ZLoc.t("Dialogs", "FactoryWorkerDialog_purchaseQuestion", {amount:this.m_factory.harvestingDefinition.workers.cashCost.toString()}),
                unlockWorkerNote:ZLoc.t("Dialogs", "FactoryWorkerDialog_purchaseNote")
            };
            return (_local1);
        }
        override protected function createDialogView(_arg1:Dictionary):GenericDialogView{
            var _local2:Array = this.m_factory.getWorkerData();
            var _local3:WorkerCellFactory = new WorkerCellFactory(_arg1, new IntDimension((680 / 2), (160 / 3)));
            var _local4:FactoryGrowStateDialogView = new FactoryGrowStateDialogView(_local2, _local3, _arg1, m_message, m_title, GenericDialogView.TYPE_CUSTOM_OK);
            _local4.addEventListener(UIEvent.ASK_FOR_CREW, this.onHireFriends, false, 0, true);
            _local4.addEventListener(UIEvent.BUY_CREW, this.onBuyWorker, false, 0, true);
            return (_local4);
        }
        protected function onContractPicLoaded(_arg1:Event):void{
            _arg1.stopImmediatePropagation();
            _arg1.target.removeEventListener(LoaderEvent.LOADED, this.onContractPicLoaded);
            this.m_contractPic = this.m_factory.harvestingDefinition.getCachedImage("growStateDialog");
            if (--this.numAssets == 0){
                onAssetsLoaded();
            };
        }
        protected function onLoadBuildableAssets(_arg1:DisplayObject, _arg2:String):void{
            m_comObject = _arg1;
            if (--this.numAssets == 0){
                onAssetsLoaded();
            };
        }
        protected function onHireFriends(_arg1:Event=null):void{
            var _local2:int = Global.experimentManager.getVariant(ExperimentDefinitions.EXPERIMENT_CREW_POPUP_MFS);
            var _local3:String = ("factoryWorker.php?wid=" + this.m_factory.getId().toString());
            if (_local2 == ExperimentDefinitions.TRAY_POPUP_MFS){
                FrameManager.showTray(_local3);
                this.close();
            } else {
                FrameManager.navigateTo(_local3);
            };
        }
        protected function onBuyWorker(_arg1:Event=null):Boolean{
            var _local2:Boolean;
            var _local3:int = this.m_factory.harvestingDefinition.workers.cashCost;
            if (Global.player.canBuyCash(_local3, true)){
                _local2 = true;
                if (this.canStillBuyWorkers() == true){
                    this.m_factory.purchaseWorker();
                    this.refresh();
                };
            };
            return (_local2);
        }
        protected function canStillBuyWorkers():Boolean{
            var _local1:int = Global.factoryWorkerManager.getWorkers(this.m_factory.getWorkerBucket()).getRemainingSpots();
            return ((_local1 > 0));
        }
        protected function getJobDescription():String{
            var _local1:int = Global.factoryWorkerManager.getWorkers(this.m_factory.getWorkerBucket()).getRemainingSpots();
            if (_local1 > 0){
                return (ZLoc.t("Items", (this.m_factory.harvestingDefinition.name + "_description"), {workersLeft:_local1}));
            };
            return (ZLoc.t("Items", (this.m_factory.harvestingDefinition.name + "_complete")));
        }

    }
}//package Modules.factory.ui 
